﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyRapChieuPhim.BUS;
using QuanLyRapChieuPhim.DTO;
using QuanLyRapChieuPhim;

namespace QuanLyRapChieuPhim.GUI
{
    public partial class uscDanhSachSuatChieu : UserControl
    {
        private SuatChieuBUS BUS;
        private SuatChieuDTO DTO;
        private BindingList<SuatChieuDTO> DanhSach;
        private bool IsLoaded = false;

        private int UserKind = 0;
        private int UserID = 0;
        private string UserName = "";

        public uscDanhSachSuatChieu()
        {
            InitializeComponent();
        }

        public uscDanhSachSuatChieu(string _UserName, int _UserID, int _UserKind)
        {
            InitializeComponent();
            
            UserName = _UserName;
            UserID = _UserID;
            UserKind = _UserKind;
        }

        private void uscDanhSachSuatChieu_Load(object sender, EventArgs e)
        {
            IsLoaded = false;
            dgvVeDaDat.Visible = false;
            pnVeDaDat.Visible = false;
            RapBUS bus_rap = new RapBUS();
            cbRap.DataSource = bus_rap.GetList();
            cbRap.DisplayMember = "TenRap";
            cbRap.ValueMember = "MaRap";
            
            LoadDanhSach();
            Setup_dgv();

            if (UserKind != 6)
            {
                btnDatVe.Visible = false;
                cbDayGhe.Visible = false;
                cbHangGhe.Visible = false;

                lbDayGhe.Visible = false;
                lbHangGhe.Visible = false;

            }
            else
            {
                btnDatVe.Visible = true;
                cbDayGhe.Visible = true;
                cbHangGhe.Visible = true;

                lbDayGhe.Visible = true;
                lbHangGhe.Visible = true;
                pnVeDaDat.Visible = true;
                dgvVeDaDat.Visible = true;
                dgvVeDaDat.DataSource = new TTDatVeBUS().GetList_KhachHang(UserID);
            }

            IsLoaded = true;
        }

        private void btnTTChiTiet_Click(object sender, EventArgs e)
        {

        }

        private void Setup_dgv()
        {
            dgvDanhSach.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            dgvDanhSach.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dgvDanhSach.ColumnHeadersDefaultCellStyle.Font = new Font(dgvDanhSach.Font, FontStyle.Bold);
            dgvDanhSach.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvDanhSach.GridColor = Color.DarkGray;     // Màu các của lưới 
            dgvDanhSach.BackgroundColor = Color.White;   // Màu nền gridview 
            dgvDanhSach.ForeColor = Color.Blue;          // Màu chữ 

            dgvDanhSach.AlternatingRowsDefaultCellStyle.ForeColor = Color.Green;
            dgvDanhSach.AllowUserToAddRows = false;
            dgvDanhSach.AllowUserToDeleteRows = false;
            dgvDanhSach.AllowUserToResizeRows = false;
            dgvDanhSach.RowHeadersVisible = false;
            // Đặt tên các cột ;
            DataGridViewCheckBoxColumn clChon = new DataGridViewCheckBoxColumn();
            clChon.Name = "Chon";
            clChon.TrueValue = "Yes";
            clChon.FalseValue = "No";
            //dgvDanhSach.Columns.Insert(0, clChon);

            ////dgvDanhSach.Columns["Chon"].HeaderText = "Chọn";
            dgvDanhSach.Columns["MaSuatChieu"].HeaderText = "Mã suất";
            dgvDanhSach.Columns["TenSuatChieu"].HeaderText = "Tên suất";
            dgvDanhSach.Columns["MaCaChieu"].HeaderText = "Mã ca";
            dgvDanhSach.Columns["MaPhongChieu"].HeaderText = "Mã phòng";
            dgvDanhSach.Columns["MaPhim"].HeaderText = "Mã phim";
            dgvDanhSach.Columns["SoLuongVe"].HeaderText = "Số vé";
            dgvDanhSach.Columns["TenPhim"].HeaderText = "Tên phim";
            dgvDanhSach.Columns["TenCaChieu"].HeaderText = "Ca chiếu";

            //dgvDanhSach.Columns["Chon"].DisplayIndex = 0;
            dgvDanhSach.Columns["TenSuatChieu"].DisplayIndex = 0;
            dgvDanhSach.Columns["SoLuongVe"].DisplayIndex = 1;
            dgvDanhSach.Columns["TenPhim"].DisplayIndex = 2;
            dgvDanhSach.Columns["TenCaChieu"].DisplayIndex = 3;
            dgvDanhSach.Columns["MaSuatChieu"].DisplayIndex = 4;
            dgvDanhSach.Columns["MaCaChieu"].DisplayIndex = 5;
            dgvDanhSach.Columns["MaPhongChieu"].DisplayIndex = 6;
            dgvDanhSach.Columns["MaPhim"].DisplayIndex = 7;
            dgvDanhSach.Columns["NgayChieu"].DisplayIndex = 8;

            //dgvDanhSach.Columns["Chon"].Visible = true;
            dgvDanhSach.Columns["TenSuatChieu"].Visible = true;
            dgvDanhSach.Columns["SoLuongVe"].Visible = true;
            dgvDanhSach.Columns["TenPhim"].Visible = true;
            dgvDanhSach.Columns["TenCaChieu"].Visible = true;
            dgvDanhSach.Columns["MaSuatChieu"].Visible = false;
            dgvDanhSach.Columns["MaCaChieu"].Visible = false;
            dgvDanhSach.Columns["MaPhongChieu"].Visible = false;
            dgvDanhSach.Columns["MaPhim"].Visible = false;
            dgvDanhSach.Columns["DaXoa"].Visible = false;

            //dgvDanhSach.Columns["Chon"].Width = 45;
            dgvDanhSach.Columns["TenSuatChieu"].Width = 145;
            dgvDanhSach.Columns["SoLuongVe"].Width = 60;
            dgvDanhSach.Columns["TenPhim"].Width = 280;
            dgvDanhSach.Columns["TenCaChieu"].Width = 145;
            dgvDanhSach.Columns["MaSuatChieu"].Width =45;
            dgvDanhSach.Columns["MaCaChieu"].Width =45;
            dgvDanhSach.Columns["MaPhongChieu"].Width = 45;
            dgvDanhSach.Columns["MaPhim"].Width = 45;

            //dgvDanhSach.Columns["Chon"].ReadOnly = false;
            dgvDanhSach.Columns["TenSuatChieu"].ReadOnly = true;
            dgvDanhSach.Columns["SoLuongVe"].ReadOnly = true;
            dgvDanhSach.Columns["TenPhim"].ReadOnly = true;
            dgvDanhSach.Columns["TenCaChieu"].ReadOnly = true;
            dgvDanhSach.Columns["MaSuatChieu"].ReadOnly = true;
            dgvDanhSach.Columns["MaCaChieu"].ReadOnly = true;
            dgvDanhSach.Columns["MaPhongChieu"].ReadOnly = true;
            dgvDanhSach.Columns["MaPhim"].ReadOnly = true;

            dgvDanhSach.Columns["NgayChieu"].HeaderText = "Ngày chiếu";
            dgvDanhSach.Columns["NgayChieu"].Visible = true;
            dgvDanhSach.Columns["NgayChieu"].Width = 100;
            dgvDanhSach.Columns["NgayChieu"].ReadOnly = true;
            dgvDanhSach.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dgvDanhSach.MultiSelect = false;

            DataGridViewTextBoxColumn cl = new DataGridViewTextBoxColumn();
            cl.HeaderText = "STT";
            cl.Name = "STT";
            cl.Width = 35;
            cl.ReadOnly = true;
            //dgvDanhSach.Columns.Insert(0, cl);
        }

        private void Update_STT()
        {
            int i = 1;
            foreach (DataGridViewRow row in dgvDanhSach.Rows)
            {
                row.Cells["STT"].Value = i;
                i++;
            }
        }

        private void LoadDanhSach()
        {
            BUS = new SuatChieuBUS();
            DanhSach = BUS.GetList();
            dgvDanhSach.DataSource = DanhSach;
        }

        private void dgvDanhSach_SelectionChanged(object sender, EventArgs e)
        {
            IsLoaded = false;
            if (dgvDanhSach.SelectedRows.Count > 0)
            {
                DTO = DanhSach[dgvDanhSach.SelectedRows[0].Index];
                lbTenCa.Text = DTO.TenCaChieu;
                lbTenPhim.Text = DTO.TenPhim;
                lbTenSuat.Text = DTO.TenSuatChieu;
                int SoLuongDaBan = new SuatChieuBUS().DemVeDaBan(DTO.MaSuatChieu);
                lbTinhTrangVe.Text = (DTO.SoLuongVe - SoLuongDaBan).ToString() + '/' + DTO.SoLuongVe.ToString();

                try
                {
                    int masuat = int.Parse(dgvDanhSach.SelectedRows[0].Cells["MaSuatChieu"].Value.ToString());

                    cbHangGhe.DataSource = new VeBUS().GetList_Hang(masuat);
                    cbHangGhe.DisplayMember = "HangGhe";
                    cbHangGhe.ValueMember = "HangGhe";
                    cbHangGhe.SelectedIndex = 0;

                    cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                    cbDayGhe.DisplayMember = "DayGhe";
                    cbDayGhe.ValueMember = "DayGhe";
                    cbDayGhe.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    return;
                }
            }
            IsLoaded = true;
        }

        private void cbRap_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                BUS = new SuatChieuBUS();
                int rap = (int)cbRap.SelectedValue;
                DanhSach = BUS.GetList_FromRap(rap);
                dgvDanhSach.DataSource = DanhSach;
            }
        }

        private void cbHangGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                int masuat = int.Parse(dgvDanhSach.SelectedRows[0].Cells["MaSuatChieu"].Value.ToString());

                cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                cbDayGhe.DisplayMember = "DayGhe";
                cbDayGhe.ValueMember = "DayGhe";
                cbDayGhe.SelectedIndex = 0;
                if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
                {
                    lbThongBao.Visible = true;
                    return;
                }
            }
        }

        private void btnDatVe_Click(object sender, EventArgs e)
        {
            TTDatVeBUS BUS = new TTDatVeBUS();

            int masuat = int.Parse(dgvDanhSach.SelectedRows[0].Cells["MaSuatChieu"].Value.ToString());

            int MaVe = new VeBUS().GetIDFromSuatHangDay
                (
                    masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)
                );
            if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
            {
                lbThongBao.Visible = true;
                return;
            }
            int Gia = new VeBUS().GetGiaVe(MaVe);
            TTDatVeDTO DTO = new TTDatVeDTO
                (
                    0, UserID,
                    MaVe, Gia, DateTime.Now
                );
            if (BUS.Add(DTO))
            {
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

        }

        private void cbDayGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                lbThongBao.Visible = false;
                int masuat = int.Parse(dgvDanhSach.SelectedRows[0].Cells["MaSuatChieu"].Value.ToString());

                if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
                {
                    lbThongBao.Visible = true;
                    return;
                }
            }
        }

        private void btnThongTinPhim_Click(object sender, EventArgs e)
        {
            if (dgvDanhSach.SelectedRows.Count > 0)
            {
                frmThongTinPhim frm = new frmThongTinPhim();
                frmThongTinPhim.Ma = int.Parse(dgvDanhSach.SelectedRows[0].Cells["MaPhim"].Value.ToString());
                frm.ShowDialog();
            }
        }

    }
}
